#include "sm3_test.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "debug.h"
#include "sm3.h"

void test_sm3()
{
    printf("****** test sm3 *******\n\n");
    const char msg[9] = "sm3 test";

    printf("msg: %s\n", msg);
    printf("\n------ test 1 : 3 step ------- \n");
    unsigned char hash[SM3_DIGEST_LENGTH];

    sm3_ctx ctx;
    sm3_init(&ctx);
    sm3_update(&ctx, (const unsigned char*)msg, sizeof(msg) - 1);
    sm3_final(&ctx, hash);

    printHex("hash", hash, SM3_DIGEST_LENGTH);

    printf("\n------ test2 : 1 step ------\n");
    sm3((const unsigned char*)msg, sizeof(msg) - 1, hash);

    printHex("hash", hash, SM3_DIGEST_LENGTH);

    int ret = access("/usr/bin/openssl", X_OK);
    if (ret) {
        printf("unsupport openssl\n\n");
        return;
    }
    printf("\nopenssl result:\n");
    system("echo -n \"sm3 test\" | openssl dgst -sm3 ");
    printf("\n\n");
}

void test_sm3_std1()
{
    printf("****** std test of sm3 (“abc” input) *******\n\n");
    unsigned char std_in[]                   = {0x61, 0x62, 0x63};
    unsigned char std_out[SM3_DIGEST_LENGTH] = {
        0x66, 0xc7, 0xf0, 0xf4, 0x62, 0xee, 0xed, 0xd9, 0xd1, 0xf2, 0xd4,
        0x6b, 0xdc, 0x10, 0xe4, 0xe2, 0x41, 0x67, 0xc4, 0x87, 0x5c, 0xf2,
        0xf7, 0xa2, 0x29, 0x7d, 0xa0, 0x2b, 0x8f, 0x4b, 0xa8, 0xe0};

    unsigned char hash1[SM3_DIGEST_LENGTH] = {0};
    unsigned char hash2[SM3_DIGEST_LENGTH] = {0};
    printf("test 1: 3 steps\n\n");

    sm3_ctx ctx;
    sm3_init(&ctx);
    sm3_update(&ctx, std_in, sizeof(std_in));
    sm3_final(&ctx, hash1);

    if (memcmp(std_out, hash1, SM3_DIGEST_LENGTH) == 0) {
        printf("- test 1 pass. (hash1 out is equal to std out)\n\n");
    } else {
        printf("- test 1 failed. (hash1 out not equal to std out)\n\n");
    }

    printf("test 2: 1 step\n\n");

    sm3(std_in, sizeof(std_in), hash2);
    if (memcmp(std_out, hash2, SM3_DIGEST_LENGTH) == 0) {
        printf("- test 2 pass. (hash2 out is equal to std out)\n\n");
    } else {
        printf("- test 2 failed. (hash2 out not equal to std out)\n\n");
    }
}

void test_sm3_std2()
{
    printf("****** std test of sm3 (64 bytes input) *******\n\n");
    unsigned char std_in[] = {
        0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63,
        0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62,
        0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61,
        0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64,
        0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63,
        0x64, 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64};
    unsigned char std_out[SM3_DIGEST_LENGTH] = {
        0xde, 0xbe, 0x9f, 0xf9, 0x22, 0x75, 0xb8, 0xa1, 0x38, 0x60, 0x48,
        0x89, 0xc1, 0x8e, 0x5a, 0x4d, 0x6f, 0xdb, 0x70, 0xe5, 0x38, 0x7e,
        0x57, 0x65, 0x29, 0x3d, 0xcb, 0xa3, 0x9c, 0x0c, 0x57, 0x32};

    unsigned char hash1[SM3_DIGEST_LENGTH] = {0};
    unsigned char hash2[SM3_DIGEST_LENGTH] = {0};
    printf("------ test 1: 3 steps ------\n\n");

    sm3_ctx ctx;
    sm3_init(&ctx);
    sm3_update(&ctx, std_in, sizeof(std_in));
    sm3_final(&ctx, hash1);

    if (memcmp(std_out, hash1, SM3_DIGEST_LENGTH) == 0) {
        printf("- test 1 pass. (hash1 out is equal to std out)\n\n");
    } else {
        printf("- test 1 failed. (hash1 out not equal to std out)\n\n");
    }

    printf("------- test 2: 1 step -------\n\n");

    sm3(std_in, sizeof(std_in), hash2);
    if (memcmp(std_out, hash2, SM3_DIGEST_LENGTH) == 0) {
        printf("- test 2 pass. (hash2 out is equal to std out)\n\n");
    } else {
        printf("- test 2 failed. (hash2 out not equal to std out)\n\n");
    }
}